package com.tianpengtech.admin.fairauth;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.im.kit.PushKit;
import com.tianpengtech.admin.AbstractAdmin;
import com.tianpengtech.admin.IAdminOpt;
import com.tianpengtech.admin.opertion.OperationDao;
import com.tianpengtech.common.model.account.Account;
import com.tianpengtech.common.model.authenticate.Authenticate;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.fairauth.FairAuth;
import com.tianpengtech.common.model.fairmessage.FairMessage;
import com.tianpengtech.common.model.fairwithdraw.FairWithdraw;
import com.tianpengtech.common.model.opertion.Opertion;
import com.tianpengtech.common.search.EqualCondition;
import com.tianpengtech.common.search.LikeCondition;
import com.tianpengtech.common.search.QueryConstructor;
import com.tianpengtech.common.util.XxxUtil;
import com.tianpengtech.common.util.push.Jpush;
import com.tianpengtech.common.validator.Validator;
import com.tianpengtech.common.validator.rules.numberic.NumbericMustBeGreaterThan;
import com.tianpengtech.common.validator.rules.string.StringLengthBetween;

public class FairAuthAdminServiceImpl extends AbstractAdmin implements IAdminOpt{

	@Override
	public void beforeAdd(Model<?> model) throws ValidateException {

	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model<?> model) throws ValidateException {

	}

	@Override
	public void beforeDelete(Model<?> model) {

	}

	@Override
	public void builderSQLExpected(Model< ? > model,
			QueryConstructor queryConstruct, Map< String, Object > params) {

		queryConstruct.and(new EqualCondition(FairAuth.DELETETAG, FairAuth.DEL_NO));
		String nickName = model.getStr(FairAuth.NICK_NAME);
		if(StringUtils.isNotBlank(nickName)){
			queryConstruct.and(new LikeCondition(FairAuth.NICK_NAME, "%"+nickName+"%"));
		}
	}

	@Override
	public String tableName() {
		return FairAuth.dao.tableName();
	}
	
	@Override
	public void changeStatus(Model<?> model, long id, int status, Kv params) throws ValidateException {
		
		String content = params.getStr(Opertion.CONTENT);
		Validator validator = new Validator();
		int price = params.getInt("price");
		
		FairAuth temp = FairAuth.dao.findById(id);
		if(temp.getInt(FairAuth.STATUS)==FairAuth.STATUS_SUCCESS){
			throw new ValidateException("当前帐号已经认证通过，不允许进行当前操作");
		}
		
		if(status==FairAuth.STATUS_FAILURE){
			validator.addRule( new StringLengthBetween(content,"拒绝理由必须是1-40个字符之间",1,40));
		}
		
		if(status==FairAuth.STATUS_SUCCESS){
			
			validator.addRule( new NumbericMustBeGreaterThan(id,0,"id值必须要大于0"));
			validator.addRule( new NumbericMustBeGreaterThan(price,1,"网红收费价格必须大于￥1"));
		}
		validator.excute();
		model.set(FairAuth.ID, id);
		model.set(FairAuth.STATUS, status);
		model.update();
		
		
		Account account = XxxUtil.readSessionInfo();
		long userId = account.getLong(Account.ID);
		
		Fair fair = Fair.dao.findById(temp.getInt(FairWithdraw.WH_ID));
		
		if(status==FairAuth.STATUS_SUCCESS ){
			String result = "【通过】你的"+temp.type()+"的认证请求";
			new FairMessage().set(FairMessage.CONTENT, "管理员"+result)
			.set(FairMessage.WH_ID, temp.getInt(FairAuth.WH_ID)).save();
			result = "【通过】"+temp.getStr(FairAuth.NICK_NAME)+"的"+temp.type()+"的认证请求";
			Jpush.pushCustomContentToSomeOne(fair.getStr(Fair.MOIBILE), result,Jpush.PUSH_MSG);
			Jpush.pushMessageInfoToSomeOne(fair.getStr(Fair.MOIBILE), result,Jpush.PUSH_MSG);
			OperationDao.ME.recordLog(id, Opertion.OPT_FAIR_AUTH_LOG,userId,result);
			
			//更新粉丝数量
			String sql = "select sum(fans) as fans from "+FairAuth.dao.tableName()+" where id=? and status=? ";
			FairAuth t= FairAuth.dao.findFirst(sql,id,FairAuth.STATUS_SUCCESS);
			long fans = t.getLong(FairAuth.FANS);
			String fansStr = XxxUtil.formatFans(fans);
			updateFair(temp,status,price,fansStr);
		}
		if(status==FairAuth.STATUS_FAILURE){
			
			String result = "【拒绝】你的"+temp.type()+"的认证请求,原因是:"+content;
			new FairMessage().set(FairMessage.CONTENT, "管理员"+result)
			.set(FairMessage.WH_ID, temp.getInt(FairAuth.WH_ID)).save();
			Jpush.pushCustomContentToSomeOne(fair.getStr(Fair.MOIBILE), result,Jpush.PUSH_MSG);
			Jpush.pushMessageInfoToSomeOne(fair.getStr(Fair.MOIBILE), result,Jpush.PUSH_MSG);
			result = "【拒绝】"+temp.getStr(FairAuth.NICK_NAME)+"的"+temp.type()+"的认证请求,原因是:"+content;
			OperationDao.ME.recordLog(id, Opertion.OPT_FAIR_AUTH_LOG,userId,result);
			
			//更新粉丝数量
			String sql = "select sum(fans) as fans from "+FairAuth.dao.tableName()+" where id=? and status=? ";
			FairAuth t= FairAuth.dao.findFirst(sql,id,FairAuth.STATUS_SUCCESS);
			long fans =0;
			if(t.get(FairAuth.FANS)!=null){
				fans = t.getLong(FairAuth.FANS);
			}
			String fansStr = XxxUtil.formatFans(fans);
			updateFair(temp,status,1,fansStr);
		}
		
	}

	private void updateFair(FairAuth fairAuth,int status,int price,String fans){
		long fairId = fairAuth.getInt(FairAuth.WH_ID);
		Fair fair = Fair.dao.findById(fairId);
		List<FairAuth> result = queryFairAuthList(fairId,Fair.STATUS_SUCCESS);
		String channelInfo = (StringUtils.isEmpty(fair.getStr(Fair.CHANNEL))?"":fair.getStr(Fair.CHANNEL));
		
		StringBuffer sb = new StringBuffer();
		//更新网红认证平台信息
		if(fairAuth.getInt(FairAuth.TYPE)==FairAuth.TYPE_WEIBO){
			sb.append(Fair.SINA);
		}
		if(fairAuth.getInt(FairAuth.TYPE)==FairAuth.TYPE_QQ){
			sb.append(Fair.QQ);
		}
		if(fairAuth.getInt(FairAuth.TYPE)==FairAuth.TYPE_WEIXIN){
			sb.append(Fair.WEIXIN);
		}
		
		if(!result.isEmpty()&& status==Fair.STATUS_SUCCESS){
			fair.set(Fair.STATUS,Fair.STATUS_SUCCESS);
			
			if(StringUtils.isEmpty(channelInfo)){
				channelInfo = sb.toString();
			}else if(!channelInfo.contains(sb.toString())){
				channelInfo = channelInfo+","+sb.toString();
			}
			fair.set(Fair.CHANNEL, channelInfo);
		}else{
			fair.set(Fair.STATUS,Fair.STATUS_FAILURE);
			if(channelInfo.contains(sb.toString())){
				
				if(channelInfo.contains(",")){
					channelInfo = channelInfo.replace(","+sb.toString(), "");
				}else{
					channelInfo = channelInfo.replace(sb.toString(), "");
				}
				
				fair.set(Fair.CHANNEL, channelInfo);
			}
		}
		fair.set(Fair.PRICE, price);
		fair.set(Fair.FANS, fans);
		fair.update();
	}
	
	private List<FairAuth> queryFairAuthList(long fairId,int status){
		String sql="select * from "+FairAuth.dao.tableName()+" where "
	+FairAuth.WH_ID+"=? and "+FairAuth.DELETETAG+" =? and "+FairAuth.STATUS+"=?";
		return FairAuth.dao.find(sql, fairId,FairAuth.DEL_NO,status);
	}
}